#!/usr/bin/env tclsh

#初始化cisco命令表
source sender.tcl
global records

proc inittable {} {
	global records
	set fid [open ./resource/ciscotable]
	set content [read $fid]
	close $fid
	set records [split $content "\n"]
}

proc translate {mycmd mylist} {
	global records
	foreach rec $records {
		set vl [string length $rec]
		if { $vl!=0 } {
			set fields [split $rec "="]
			if {[string compare $mycmd [lindex $fields 0]]==0} {
				set myrep [lindex $fields 1]
				set llen [llength $mylist];
				for {set x 0} {$x<$llen} {incr x} {
					regsub {<-val->} $myrep [lindex $mylist $x] myrep
				}
				return $myrep
			}
		}
	}
}

proc ospfConf {infList} {
	set commands [dict get $infList commands]
	foreach command $commands {
		set fields [split $command " "]
		set commandData {}
		set cmdType [lindex $fields 0]
		set llen [llength $fields]
		for {set x 1} {$x<$llen} {incr x} {
			lappend commandData [lindex $fields $x]
		}
		set cmd [translate $cmdType $commandData]
		sendCmd $cmd
		
	}
	sendCmd [translate exit {}]
}

proc bgpConf {infList} {
	
}

proc initPort {infList} {
	set portConfList [dict get $infList ports-conf]

	set llen [llength $portConfList]
	
#循环配置每个port
	for {set x 0} {$x<$llen} {incr x} {
		set portConf [lindex  $portConfList $x]
		
		set portTypeList {}
		set portType [dict get $portConf port-number]
		lappend portTypeList $portType
		
		set portInfList {}
		lappend portInfList [dict get $portConf port-ip]
		lappend portInfList [dict get $portConf port-mask]

#以下用于发送命令	
#顺序如下：
#int f0/0
#ip address
#no shutdown
#exit
		sendCmd [translate interface $portType]
		sendCmd [translate setintip $portInfList]
		
		set clock_rate [dict get $portConf clock-rate]
		if {[string compare $clock_rate "" ] != 0} {
			sendCmd [translate setclockrate $clock_rate]
		}
		
		sendCmd [translate setintend {}]
		sendCmd [translate exit {}]
	}	
}

proc ParserController {infList} {
	sendCmd "conf t"
	set protocolInf [dict get $infList protocolInfo]
	set portInfo [dict get $infList portInfo]
	set protocol [dict get $protocolInf protocol]
	initPort $portInfo
	if {[string compare $protocol "ospf"]==0} {
		ospfConf $protocolInf
	}
}

proc testController {testList} {
	set testCmd [dict get $testList commands]
	set llen [llength $testCmd]

	for {set x 0} {$x<$llen} {incr x} {
		sendCmd [lindex $testCmd $x]
	}
}